package com.fairytask.ui.item.pet
{
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.ResManager;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.greensock.TimelineLite;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 宠物蛋真言
	 * @author xujh
	 * 
	 */
	public class PetEggWordItem extends Sprite
	{
		private var word1	:WordItem;
		private var word2	:WordItem;
		private var word3	:WordItem;
		private var word4	:WordItem;
		private var itemBG	:MovieClip;	//宠物蛋背景
		private var itemClip	:Clip;	//宠物蛋图片
		private var itemLab	:Label;	//宠物蛋名称
		private var _wordSp	:Sprite = new Sprite;	//真言层
		private var _bg		:Clip;
		
		private var _canGet	:Boolean = false;	//可以获取
		private var _pos		:int;
		private var _petID		:int;
		private var _data		:ViewObject;
		//动画
		private var bgTimeLine	:TimelineLite = new TimelineLite({repeat:-1});
		private var eggTimeLine	:TimelineLite = new TimelineLite({repeat:-1});
		//初始化计数
		private var _initCount	:int	=	4;
		
		public function PetEggWordItem()
		{
			super();
			init();
		}
		
		/**
		 * 初始化 
		 * 
		 */
		private function init():void
		{
			//初始化背景
			_bg = new Clip();
			_bg.x = _bg.y = -8;
			_bg.source = ResConfig.uiBGPath + "wordsBG_4.png";
			
			addChild(_bg);
			//字初始化
			initWord();
			
			//宠物蛋
			itemBG = ResManager.newResClass("CustSkin_017") as MovieClip;
			itemBG.width = itemBG.height = 56;
			itemBG.x = itemBG.y = 40;
			addChild(itemBG);
			//宠物蛋图标
			itemClip = new Clip();
			itemClip.x = 43;
			itemClip.y = 42;
			addChild(itemClip);
			
			itemLab = new Label;
			itemLab.x = 25;
			itemLab.y = 140;
			addChild(itemLab);
			itemClip.addEventListener(MouseEvent.ROLL_OVER,onOver);;
			itemClip.addEventListener(MouseEvent.ROLL_OUT,onOut);
			
			//动画时间轴初始化
			bgTimeLine.append(TweenMax.to(_bg, 0.5, {glowFilter:{color:0xFFFFFF, alpha:0.7, blurX:20, blurY:20, strength:2, quality:1},repeat:-1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}}}));
			eggTimeLine.append(TweenMax.to(itemBG, 0.5, {glowFilter:{color:0xFFFFFF, alpha:0.7, blurX:20, blurY:20, strength:2, quality:1},repeat:-1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}}}));
			bgTimeLine.pause();
			eggTimeLine.pause();
			
		}
		
		/**
		 * 字初始化
		 * 
		 */
		private function initWord():void
		{
			addChild(_wordSp);
			word1 = new WordItem();
			word2 = new WordItem();
			word3 = new WordItem();
			word4 = new WordItem();
			word1.x = word3.x = 0;
			word2.x = word4.x = 65;
			word1.y = word2.y = 0;
			word3.y = word4.y = 65;
			_wordSp.addChild(word1);
			_wordSp.addChild(word2);
			_wordSp.addChild(word3);
			_wordSp.addChild(word4);
		}
		
		/**
		 * 显示宠物蛋tips 
		 * @param evt
		 * 
		 */
		private function onOver(evt:MouseEvent):void
		{
			if(null != _data)
			{
				TipsManage.petEggTips(_petID);
			}
		}
		
		/**
		 * 隐藏tips 
		 * @param evt
		 * 
		 */
		private function onOut(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 设置字状态 
		 * @param pos
		 * @param status
		 * 
		 */
		public function setStatus(pos:int,status:int):void
		{
			var tmpItem	:WordItem = _wordSp.getChildAt(pos) as WordItem;
			//是否特效
			if(_initCount != 0)
			{
				tmpItem.able = Boolean(status);
				_initCount--;
			}
			else
			{
				//状态
				if(Boolean(status))
				{
					//显示特效 
					bgTimeLine.play();
//					TweenMax.to(_bg, 1, {onComplete:onFlashOver,overwrite:5,glowFilter:{color:0xFFFFFF, alpha:1, blurX:30, blurY:30, strength:1, quality:1},repeat:2,yoyo:true,startAt:{overwrite:5,glowFilter:{color:0xFFFFFF, alpha:0, blurX:0, blurY:0, strength:1, quality:1}}});
//					TweenMax.to(tmpItem, 1, {overwrite:5,glowFilter:{color:0xFFFFFF, alpha:1, blurX:30, blurY:30, strength:1, quality:1},repeat:2,yoyo:true,startAt:{overwrite:5,glowFilter:{color:0xFFFFFF, alpha:0, blurX:0, blurY:0, strength:1, quality:1}}});
					//文字
					tmpItem.able = true;
					tmpItem.onFlash(onFlashOver);
				}
				else
				{
					tmpItem.able = Boolean(status);
				}
				
			}
		}
				
		/**
		 * 播放完毕 
		 * 
		 */
		private function onFlashOver():void
		{
			if(!canGet)
			{
				bgTimeLine.stop();
				_bg.filters	=	[];
			}
			else
			{
				//同步动画
				bgTimeLine.stop();
				eggTimeLine.stop();
				_bg.filters		= null;
				itemBG.filters	= null;
				bgTimeLine.play();
				eggTimeLine.play();
			}
		}
		
		/**
		 * 设置字内容 
		 * @param pos
		 * @param status
		 * 
		 */
		public function setWord(pos:int,val:int):void
		{
			var tmpItem	:WordItem = _wordSp.getChildAt(pos) as WordItem;
			tmpItem.word = val;
		}
		
		/**
		 * 设置宠物蛋id 
		 * @param val
		 * 
		 */
		public function set petEggID(val:int):void
		{
			_petID = val;
			//图标
			itemClip.source = ResConfig.iconPath + StaticIni.getIniVar(val,"Icon") + ResConfig.iconImg;
			//名称
			itemLab.htmlStrByBorder = TipsTools.link_color(StaticIni.getSwfLangStr(StaticIni.getIniVar(val,"NameID")),int(StaticIni.getIniVar(val,"Color")),16);
			itemLab.x = 70 - itemLab.width/2;
		}
		
		public function get petEggID():int
		{
			return _petID;
		}
		
		public function get pos():int
		{
			return _pos;
		}
		
		/**
		 * 初始化数据 
		 * @param val
		 * 
		 */
		public function set data(val:ViewObject):void
		{
			//设置字
			/*for(var i:int=0;i<4;i++)
			{
				var tmpItem	:WordItem = _wordSp.getChildAt(i) as WordItem;
				var _word	:String = "Pos" + (i + 1);
				var _sta	:String = "State" + (i + 1);
				tmpItem.word = val.GetPropertiesEx(_word);
				tmpItem.able = Boolean(val.GetPropertiesEx(_sta));
			}*/
			_data = val;
			if(null == val)
			{
				return;
			}
			//位置
			_pos = val.GetPropertiesEx("Pos");
			//宠物蛋id
			this.petEggID = val.GetPropertiesEx("ConfigID");
		}
		
		public function get data():ViewObject
		{
			return _data;
		}
		
		/**
		 * 是否可以获取 
		 * @return 
		 * 
		 */
		public function get canGet():Boolean
		{
			return _canGet;
		}
		
		public function set canGet(val:Boolean):void
		{
			_canGet = val;
			if(_canGet)
			{
				itemClip.buttonMode = true;
				//发光
				bgTimeLine.play();
				eggTimeLine.play();
			}
			else
			{
				itemClip.buttonMode = false;
				//取消发光
				bgTimeLine.stop();
				eggTimeLine.stop();
				_bg.filters		= null;
				itemBG.filters	= null;
			}
		}
		
		/**
		 * 清空 
		 * 
		 */
		public function clear():void
		{
			word1.clearAll();
			word2.clearAll();
			word3.clearAll();
			word4.clearAll();
			itemClip.clear();
			itemLab.htmlStrByBorder = "";
			this.canGet = false;
			_initCount = 4;
		}
	}
}